package bb.lanxing.mvp.model;

import android.content.Context;
//import com.alipay.sdk.packet.AbstractC1441d;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import bb.lanxing.BuildConfig;
import bb.lanxing.lib.devices.core.utils.DeviceHelper;
import bb.lanxing.lib.devices.sprint.entity.SprintFirmware;
import bb.lanxing.lib.devices.sprint.entity.XossGFirmware;
import bb.lanxing.lib.devices.utils.DeviceVersionHelper;
//import bb.lanxing.network.BiciHttpClient;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import bb.lanxing.network.MyHttpClient;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONException;
import org.json.JSONObject;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class XOSSGSprintServerFirmwareModel extends SprintServerFirmwareModel {
    private String hardware;
    private Subscription laodFirmwareSub;
    private String model;

    @Override
    protected List<SprintFirmware> loadFirmwares() throws IOException {
        return null;
    }

    public XOSSGSprintServerFirmwareModel(String model, String hardware) {
        this.model = model;
        this.hardware = hardware;
    }

    @Override
    public String getVersionListUrl() {
        return versionUrl();
    }

    private String versionUrl() {
        return "https://www." + MyHttpClient.DOMAIN_BASE_PURE + "/api/opendevice/firmwares/";
    }

    @Override
    public String getFirmwareDir() {
        return new File(DeviceHelper.getWorkDir(17), "fw_xoss_g").getAbsolutePath();
    }

    protected List<XossGFirmware> loadXossGFirmwares() throws IOException {
        HttpUrl.Builder newBuilder = HttpUrl.parse(getVersionListUrl()).newBuilder();
        newBuilder.addQueryParameter("device", this.model);
        newBuilder.addQueryParameter("hardware_version", this.hardware);
        newBuilder.addQueryParameter("platform", "android");
        Response execute = MyHttpClient.client.newCall(new Request.Builder().url(newBuilder.build()).get().build()).execute();
        if (execute.isSuccessful()) {
            try {
                return new GsonBuilder().create()
                        .fromJson(new JSONObject(execute.body().string()).getJSONArray("results").toString(),
                                new TypeToken<List<XossGFirmware>>() {
                }.getType());
            } catch (JSONException unused) {
            }
        }
        return null;
    }

    class C93633 implements Observable.OnSubscribe<String> {
        C93633() {
        }

        @Override
        public void call(Subscriber<? super String> subscriber) {
            try {
                List<XossGFirmware> loadXossGFirmwares = XOSSGSprintServerFirmwareModel.this.loadXossGFirmwares();
                if (subscriber.isUnsubscribed()) {
                    return;
                }
                String str = "";
                if (loadXossGFirmwares != null) {
                    int size = loadXossGFirmwares.size();
                    XossGFirmware[] xossGFirmwareArr = new XossGFirmware[size];
                    loadXossGFirmwares.toArray(xossGFirmwareArr);
                    if (size > 0) {
                        Arrays.sort(xossGFirmwareArr, new Comparator() {
                            @Override
                            public final int compare(Object obj, Object obj2) {
                                int compareTo;
                                compareTo = ((XossGFirmware) obj2).getVersion().compareTo(((XossGFirmware) obj).getVersion());
                                return compareTo;
                            }
                        });
                        boolean z = false;
                        XossGFirmware xossGFirmware = xossGFirmwareArr[0];
                        if (DeviceVersionHelper.isNeedUpdate(BuildConfig.VERSION_NAME, xossGFirmware.getMinimumClientVersion())) {
                            z = true;
                            XOSSGSprintServerFirmwareModel.this.onClientNeedUpdate(xossGFirmware.getMinimumClientVersion());
                        }
                        boolean equals = xossGFirmware.getHardwareVersion().trim().toLowerCase().equals(XOSSGSprintServerFirmwareModel.this.hardware.trim().toLowerCase());
                        if (!z && equals) {
                            XOSSGSprintServerFirmwareModel.this.onNewestFirmwareVersion(xossGFirmware.getVersion(), xossGFirmware.getDescription(), xossGFirmware.getFirmwareUrl());
                            str = xossGFirmware.getVersion();
                        }
                    }
                }
                subscriber.onNext(str);
                subscriber.onCompleted();
            } catch (IOException e) {
                subscriber.onError(e);
            }
        }
    }

    @Override
    public void requestFirmwareVersion(final Context context) {
        if (context == null) {
            return;
        }
        this.laodFirmwareSub = Observable.create(new C93633())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onNext(String s) {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }
                });
    }

    @Override
    public void release() {
        super.release();
        Subscription subscription = this.laodFirmwareSub;
        if (subscription != null) {
            subscription.unsubscribe();
            this.laodFirmwareSub = null;
        }
    }
}
